package com.ruoyi.web.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.otc.domain.OperationLog;
import com.ruoyi.otc.domain.User;
import com.ruoyi.otc.service.IOperationLogService;
import com.ruoyi.otc.service.IUserService;
import com.ruoyi.web.api.base.ApiBaseController;
import com.ruoyi.web.api.vo.UserVo;
import com.ruoyi.web.constant.ApiConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 版本V1接口
 * @author LSQ
 */
@Api(value = "系统用户信息接口",tags = "系统用户信息接口")
@RestController
@RequestMapping(ApiConstant.API_V1+"user")
public class ApiUserController extends ApiBaseController {
    @Autowired
    private IUserService userService;
    @Autowired
    private IOperationLogService operationLogService;


    /**
     * 新增保存系统用户信息
     */
    @ApiOperation("新增保存系统用户信息")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@RequestBody UserVo user)
    {
        List<User> list = userService.list(new QueryWrapper<User>().eq("login_name", user.getLoginName()));
        if (list.size()>0){
            return AjaxResult.error("该用户已存在");
        }
        User user1 = new User();
        user1.setPassword(user.getPassword());
        user1.setLoginName(user.getLoginName());
        user1.setCreateTime(new Date());
        user1.setUpdateTime(new Date());
        return toAjax(userService.insertUser(user1));
    }


    /**
     * 修改用户密码
     */
    @ApiOperation("修改用户密码")
    @PostMapping("/updatePass")
    @ResponseBody
    public AjaxResult updatePass(@RequestBody UserVo user)
    {
        User user1= userService.getOne(new QueryWrapper<User>().eq("login_name", user.getLoginName()));
        user1.setPassword(user.getPassword());
        user1.setUpdateTime(new Date());
        userService.updateById(user1);
        return AjaxResult.success();
    }

    /**
     * 删除用户
     */
    @ApiOperation("删除用户")
    @PostMapping("/deleteUser/{id}")
    @ResponseBody
    public AjaxResult deleteUser(@PathVariable("id") Long id)
    {
        userService.deleteUserById(id);
        return AjaxResult.success();
    }

    /**
     * 查询系统用户信息列表
     */
    @ApiOperation("查询系统用户信息列表")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestBody UserVo user)
    {
        startPage();
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(user.getLoginName()),"login_name",user.getLoginName());
        List<User> list = userService.list(wrapper);
        return getDataTable(list);
    }

    /**
     * 系统用户登录
     */
    @ApiOperation("后台登录")
    @PostMapping("/login")
    @ResponseBody
    public AjaxResult userLogin(@RequestBody UserVo user)
    {
        User loginUser = userService.getOne(new QueryWrapper<User>()
                .eq("login_name", user.getLoginName())
                .eq("password",user.getPassword()));
        saveLog(loginUser,user.getLoginName());
        return loginUser == null ? error("用户账号或密码错误") : success();
    }

    private void saveLog(User loginUser,String userName) {
        OperationLog operationLog = new OperationLog();
        if (loginUser == null) {
            operationLog.setResult("登录失败");
        } else {
            operationLog.setResult("登录成功");
        }
        operationLog.setUserName(userName);
        operationLog.setOptDescribe("登录");
        operationLog.setOptTime(new Date());
        operationLogService.save(operationLog);
    }


}
